<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 广告申请记录模块
 *
 * @author www.dingwei.cn
 */
class MerchantAdsModel extends MY_Model{
	
	private $tbMerAds = 'merchant_ads';
    public $tbUser = 'merchant';
    public $tbShop = 'merchant_shop';
    public $tbAds = 'admin_ads';

    /**
     * 获取列表数据
     * @param   $merchantId     int
     * @param   array           $ci
     * @return 	array
     * @author heyu
     */
    public function adminPcGetList($merchantId = 0, $ci = array()){
        $adPosition = (int)get('ad_position');
        $name = trim(get('name'));
        $status = (int)get('ads_status');
        $send_date_from = get('ads_send_date_to');
        $send_date_to = get('ads_send_date_to');
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if (!empty($ci)){
            $this->db->where_in('s.county', $ci);
        }
        if ($merchantId){
            $this->db->where(array('a.merchant_id' => $merchantId));
        }
        if ($adPosition){
            $this->db->where(array('a.ad_position' => $adPosition));
        }
        if ($name){
            $this->db->like(array('u.shop_name' => $name));
        }
        if ($status){
            $this->db->where(array('a.status' => $status));
        }
        if (strlen($send_date_from) && strlen($send_date_to)){
            $this->db->where(array('a.addtime >= ' => strtotime($send_date_from), 'a.addtime <= ' => strtotime($send_date_to)));
        }
        if ($iSortCol_0 == 1){
            $this->db->order_by('a.day', $sSortDir_0); // 申请周期
        }else if($iSortCol_0 == 2){
            $this->db->order_by('a.addtime', $sSortDir_0); // 申请时间
        }

        $this->db->select('a.id,a.merchant_id,a.ad_position,a.day,a.status,a.note,a.addtime,s.shop_name,ads.endtime')
            ->join($this->tbUser.' u', 'a.merchant_id=u.id')
            ->join($this->tbShop.' s','u.id = s.merchant_id', 'left')
            ->join($this->tbAds.' ads','ads.related_id = a.id', 'left')
            ->order_by('a.id desc');
        $data = $this->pagination($this->tbMerAds.' a');
        return $data;
 	}

    /**
     * 获取广告详情
     * @param $id
     * @param $merchantId
     * @return array
     * @author heyu
     */
 	public function getInfo($id, $merchantId = 0){
        if (!$id){
            return array();
        }

        if($merchantId){
            $this->db->where('a.merchant_id', $merchantId);
        }
        $this->db->select('a.id,a.merchant_id,a.ad_position,a.day,a.status,u.nickname,s.shop_name')
            ->join($this->tbUser.' u', 'a.merchant_id = u.id')
            ->join($this->tbShop.' s','u.id = s.merchant_id', 'left')
            ->where(array('a.id' => $id));

        $tA = $this->getOne($this->tbMerAds.' a');
        return $tA;
    }

    /**
     * 获取商家广告位申请记录
     * @param $merchantId  商户id
     * @param $page  页码
     * @return array
     */
    public function appGetApplyList($merchantId, $page){
 	    if(!$merchantId)return array();

        get_instance()->loadModel('AdmAdsModel','mAdm','admin');

        $this->db->select('a.id,a.ad_position,a.day,a.status,a.note,s.starttime,s.endtime')
                 ->join($this->tbUser.' u', 'a.merchant_id = u.id')
                 ->join($this->mAdm->tbAds.' s', 's.related_id = a.id', 'left')
                 ->where(array('a.merchant_id' => $merchantId,'a.is_delete' => 1))
                 ->group_by('a.id')
                 ->order_by('a.addtime desc');
        $tA = $this->appPage($this->tbMerAds.' a', $page);

        foreach ($tA['list'] as $k=>$v){
            $tA['list'][$k]['ad_position'] = $this->adsPosition($v['ad_position']);

            if($v['endtime'] && $v['endtime'] < time()){//过期
                $tA['list'][$k]['status'] = 3;
            }elseif(!isset($v['starttime']) && $v['status'] == 1){//申请中
                $tA['list'][$k]['status'] = 1;
            }elseif($v['endtime'] && $v['endtime'] > time()){//投放中
                $tA['list'][$k]['status'] = 2;
            }
            $tA['list'][$k]['status_desc'] = $this->getStatus($tA['list'][$k]['status'], false);
            $tA['list'][$k]['time'] = $v['starttime']?dateFormat($v['starttime']).'-'.dateFormat($v['endtime']):'';
            $tA['list'][$k]['note'] = $v['note']?$v['note']:'';
            unset( $tA['list'][$k]['starttime'], $tA['list'][$k]['endtime']);
        }
       return $tA;
    }

    /**
     * 可申请的广告位
     * @param int $key  广告位
     * @return array
     */
    public function adsPosition($key = 0){

        $data = array(
            array('ads_position' => 1,'name' => '首页'),
            array('ads_position' => 6,'name' => '商盟顶部轮播'),
            array('ads_position' => 7,'name' => '商盟中部左边'),
            array('ads_position' => 8,'name' => '商盟中部右上'),
            array('ads_position' => 9,'name' => '商盟中部右下')
        );

        if(!$key){
            return $data;
        }

        foreach ($data as $k=>$v){
            if($v['ads_position'] == $key){
                return $v['name'];
            }
        }

        return '';
    }
    /**
     * 根据广告位查询
     * @param $position
     * @return array
     * @author heyu
     */
    public function getInfoByPosition($position){
        if (!$position){
            return array();
        }
        $this->db->select('id,ads_name,ads_position,endtime')
            ->where(array('ads_position' => $position));

        $tA = $this->getAll($this->tbMerAds);
        return $tA;
    }

 	/**
 	 * 保存信息
 	 * @param 	array 	$data
 	 * @param   integer $id
 	 * @return	boolean|integer
     * @author heyu
 	 */
 	public function save($data, $id = 0){
 		if($id){
 			$this->db->where('id', $id);
 			return $this->update($this->tbMerAds, $data);
 		}

 		return $this->insert($this->tbMerAds, $data);
 	}

    /**
     * 删除
     * @param $id
     * @param int $merchantId 商户id
     * @return bool
     * @author heyu
     */
    public function deleteAds($id, $merchantId = 0){
        if (!$id){
            return false;
        }
        if($merchantId){
            $this->db->where('merchant_id', $merchantId);
        }
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbMerAds);
    }

    /**
     * 批量保存
     * @param $data
     * @param $key
     * @return bool
     */
    public function saveBatch($data, $key){
        if ($key){
            return $this->updateBatch($this->tbNewsType, $data, $key);
        }
        return $this->insertBatch($this->tbNewsType, $data);
    }


    /**
     * 获取状态显示样式
     * @param 	integer $status
     * @param 	bool    $show
     * @return	string
     */
    public function getStatus($status = null, $show = false){

        $config = array(1=>'申请中', 2 => '投放中', 3 => '已过期',  4 => '已拒绝',  5 => '已取消');
        if( ! $status){
            return $config;
        }

        if($status && ! isset($config[$status])){
            return '';
        }

        if( ! $show){
            return $config[$status];
        }


        if($status == 1){
            return '<span class="label label-primary">'.$config[$status].'</span>';
        }else if($status == 2){
            return '<span class="label label-success">'.$config[$status].'</span>';
        }else if($status == 3){
            return '<span class="label label-danger">'.$config[$status].'</span>';
        }else if($status == 4){
            return '<span class="label label-danger">'.$config[$status].'</span>';
        }else if($status == 5){
            return '<span class="label label-default">'.$config[$status].'</span>';
        }

        return'';
    }

}